package com.qiwx.pigeon.helper;

import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.graphics.drawable.AnimationDrawable;
import android.text.TextUtils;
import android.view.Display;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.qiwx.pigeon.R;
import com.qiwx.pigeon.base.BaseActivity;


/**
 * @author: zhouzc
 * @time: 2017/7/9 17:19
 * @desc:
 */
public class LoadingDialog {
    private static final String TAG = "LoadingDialog";
    private static Dialog mLoadingDialog;

    //private static AnimationDrawable animationDrawable;
//    private static ImageView animationIV;

    public static Dialog showLoading(final Activity context, String msg, boolean cancelable) {
        disDialog();//先cancel
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_loading, null);
        TextView loadingText = (TextView) view.findViewById(R.id.id_tv_loading_dialog_text);
//        animationIV = (ImageView) view.findViewById(R.id.img_anim);
//
//        animationIV.setImageResource(R.drawable.loading_anim);
//        animationDrawable = (AnimationDrawable) animationIV.getDrawable();
//        animationDrawable.start();

        if(TextUtils.isEmpty(msg)){
            loadingText.setText("");
            loadingText.setVisibility(View.GONE);
        }else{
            loadingText.setText(msg);
            loadingText.setVisibility(View.VISIBLE);
        }
        mLoadingDialog = new Dialog(context, R.style.CustomProgressDialog);
        setWH(mLoadingDialog);
        mLoadingDialog.setCancelable(cancelable);
        mLoadingDialog.setCanceledOnTouchOutside(false);
        mLoadingDialog.setContentView(view, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
        mLoadingDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if(event.getKeyCode() == KeyEvent.KEYCODE_BACK){
                    if(context instanceof BaseActivity){
                        ((BaseActivity)context).forceDismissLoading();
                    }
                }
                return false;
            }
        });
        mLoadingDialog.show();
        return mLoadingDialog;
    }

    public static Dialog showLoading(Activity context) {
        return showLoading(context, null, true);
    }

    /**
     * 关闭加载对话框
     */
    public static void disDialog() {
        if (mLoadingDialog != null && mLoadingDialog.isShowing()) {
            //animationIV.setImageBitmap(null);
            //animationDrawable.stop();

            mLoadingDialog.dismiss();
            mLoadingDialog = null;
        }
    }

    private static Dialog setWH(Dialog dialog) {
        if(dialog.getOwnerActivity()==null){
            return dialog;
        }

        /*
         * 将对话框的大小按屏幕大小的百分比设置
         */
        WindowManager m = dialog.getOwnerActivity().getWindowManager();
        Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用
        Window dialogWindow = dialog.getWindow();
        WindowManager.LayoutParams p = dialogWindow.getAttributes(); // 获取对话框当前的参数值
        p.width = (int) (d.getWidth() * 0.25);
        p.height = (int) (d.getWidth() * 0.25);
        return dialog;
    }
}